package cn.iocoder.mall.resource.utils;
import cn.iocoder.common.utils.common.api.vo.Result;
import cn.iocoder.common.utils.common.system.vo.ImageUri;
import cn.iocoder.common.utils.common.util.UUIDGenerator;
import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class MinIOUtils {

    @Value("${minio.endPoint}")
    private String  endPoint;
    @Value("${minio.port}")
    private Integer  port;
    @Value("${minio.accessKey}")
    private String accessKey;
    @Value("${minio.secretKey}")
    private String secretKey;

    @Value("${minio.bucketName}")
    private String  bucketName;
    private  static final long  expireTime=86400 * 7;

    private  static final String  imagePath="mall:resource:image";
    @Autowired
    private RedisUtil redisUtil;

    private static MinioClient instance ;
    public MinioClient getInstance() throws InvalidPortException, InvalidEndpointException {
        if (instance == null) {
            instance = new MinioClient(endPoint,port,accessKey,secretKey);
        }
        return instance;
    }

    public String  getImageByUri(String uri) throws Exception  {

        Object o=redisUtil.get(imagePath+uri);
        if (o!=null){
            return  (String)o;
        }

        String result=getInstance().presignedGetObject(bucketName,uri);
        redisUtil.set(imagePath+uri,result,expireTime);
        return result ;
    }


    public Result<?> uploadImage(MultipartFile multipartFile){
        String fileName=multipartFile.getOriginalFilename();
        ImageUri imageUri=new ImageUri();
        String fileCode= UUIDGenerator.generate()+fileName.substring(fileName.lastIndexOf('.'));
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Content-Type", "application/octet-stream");

        headerMap.put("X-Amz-Meta-Key", "meta-data");
        try {
        KeyGenerator symKeyGenerator = KeyGenerator.getInstance("AES");
        symKeyGenerator.init(256);
        InputStream inputStream=multipartFile.getInputStream();
        Long l= Long.valueOf(inputStream.available());
        SecretKey symKey = symKeyGenerator.generateKey();

            getInstance().putObject(bucketName,fileCode,inputStream,l,"application/octet-stream");
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return Result.error("保存失败");
        }
        imageUri.setImageUri(fileCode);
        return Result.ok(imageUri);
    }

    public List<ImageUri> getUriList(List<String> strings) throws Exception {

        List<ImageUri> list=new ArrayList<>();
        ImageUri uri;
        for (String s:strings){
            uri=new ImageUri();
            uri.setImageUri(getInstance().presignedGetObject(bucketName,s));
            list.add(uri);
        }

        return list;
    }

}
